package com.centling.smartSealForPhone.activity

import android.content.Intent
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.TextView
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.utils.DisplayUtil
import com.centling.smartSealForPhone.utils.ShowToast
import com.jzxiang.pickerview.TimePickerDialog
import com.jzxiang.pickerview.data.Type
import kotlinx.android.synthetic.main.activity_history_search.*
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class HistorySearchActivity : TitleBarActivity() {

    private var status = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_history_search)

        setTitleBarText("搜索")

        initView()
    }

    private fun initView() {
        tv_history_search_time_begin.setOnClickListener { v -> showDatePicker(v as TextView) }
        tv_history_search_time_end.setOnClickListener { v -> showDatePicker(v as TextView) }
        tv_history_search_search.setOnClickListener { checkInfoToSearch() }

        val items = arrayOf("--不限--", "审批中", "通过", "拒绝", "未审批通过", "审批后过期", "用章完成", "复审成功", "复审失败", "补拍完成", "审批驳回")
        val adapter = object : ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items) {
            override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
                val view = super.getView(position, convertView, parent)
                (view as TextView).textSize = 17f
                return view
            }

            override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
                val view = super.getView(position, convertView, parent)
                view.setPadding(DisplayUtil.dp2px(16f), DisplayUtil.dp2px(16f),
                        DisplayUtil.dp2px(16f), DisplayUtil.dp2px(16f))
                (view as TextView).textSize = 17f
                return view
            }
        }
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        sp_history_search_status.adapter = adapter
        sp_history_search_status.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                when (position) {
                    0 -> status = ""
                    1 -> status = "0"
                    2 -> status = "1"
                    3 -> status = "2"
                    4 -> status = "3"
                    5 -> status = "4"
                    6 -> status = "5"
                    7 -> status = "6"
                    8 -> status = "7"
                    9 -> status = "8"
                    10 -> status = "9"
                }
            }
        }
    }

    private fun showDatePicker(textView: TextView) {
        TimePickerDialog.Builder().setCallBack { _, millseconds ->
            val time = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(Date(millseconds))
            textView.text = time
        }       .setCancelStringId("取消")
                .setSureStringId("确定")
                .setTitleStringId("请选择时间")
                .setYearText("年")
                .setMonthText("月")
                .setDayText("日")
                .setHourText("时")
                .setMinuteText("分")
                .setCyclic(false)
                .setMinMillseconds(SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).parse("2016-01-01").time)
                .setCurrentMillseconds(System.currentTimeMillis())
                .setThemeColor(ContextCompat.getColor(mContext, R.color.blue1))
                .setType(Type.YEAR_MONTH_DAY)
                .setWheelItemTextNormalColor(ContextCompat.getColor(mContext, R.color.grey1))
                .setWheelItemTextSelectorColor(ContextCompat.getColor(mContext, R.color.blue1))
                .setWheelItemTextSize(17)
                .build()
                .show(supportFragmentManager, "year_month_day")
    }

    private fun checkInfoToSearch() {
        if (TextUtils.isEmpty(et_history_search_history_content.text.toString()) && TextUtils.equals(
                tv_history_search_time_begin.text.toString(), "请选择开始时间") && TextUtils.equals(
                tv_history_search_time_end.text.toString(), "请选择结束时间")) {
            ShowToast.shortTime("请至少选择一个搜索条件")
            return
        }
        if (!TextUtils.equals(tv_history_search_time_begin.text.toString(), "请选择开始时间")
                && !TextUtils.equals(tv_history_search_time_end.text.toString(), "请选择结束时间")) {
            var timeBegin = 0L
            var timeEnd = 0L
            val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
            try {
                timeBegin = sdf.parse(tv_history_search_time_begin.text.toString()).time
                timeEnd = sdf.parse(tv_history_search_time_end.text.toString()).time
            } catch (e: ParseException) {
                e.printStackTrace()
            }

            if (timeBegin > timeEnd) {
                ShowToast.shortTime("开始时间不能大于结束时间")
                return
            }
        }

        val intent = Intent(this@HistorySearchActivity,
                HistorySearchResultActivity::class.java)
        intent.putExtra("content", et_history_search_history_content.text.toString())
        intent.putExtra("status", status)
        intent.putExtra("begin", if (TextUtils.equals(tv_history_search_time_begin.text.toString(), "请选择开始时间"))
            ""
        else
            tv_history_search_time_begin.text.toString())
        intent.putExtra("end", if (TextUtils.equals(tv_history_search_time_end.text.toString(), "请选择结束时间"))
            ""
        else
            tv_history_search_time_end.text.toString())
        startActivity(intent)
    }
}
